// pages/channel/view/view.js
// 引入 QCloud 小程序增强 SDK
var qcloud = require('../../../vendor/qcloud-weapp-client-sdk/index');

// 引入配置
var config = require('../../../config');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => {
    wx.hideToast();
    wx.showToast({
        title: text,
        icon: 'success'
    });
};

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    });
};

var showMsg = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: content,
        showCancel: false
    });
};
//获取应用实例
var app = getApp()
Page({
  data: {
      hash_key:"",
      loading:false,
      start:false,
      text:"",
      is_mine:false,
      imageSrc:"",
      nicker_name:"",
      animationData: {},
      num:5,
      video_hidden:true,
  },
  Interval:function(){
       var self=this;
        this.data.intervarID= setInterval(function(){
                self.animation.scale(2, 2).step()
                self.animation.scale(1, 1).step()
                self.setData({
                    animationData: self.animation.export()
                })
                self.setData({
                    num:self.data.num-1,
                });
                if(self.data.num == 0){
                    clearInterval(self.data.intervarID);
                    setTimeout(function(){
                        wx.switchTab({
                            url: '/pages/channel/index/index'
                        })
                    }, 1000);
                }
            },1000);
  },
  onLoad: function (e) {
    console.log('onLoad');
    app.getUserInfo(function(userInfo){
      console.log(userInfo);
    });
    this.setData({
        hash_key:e.hash_key
    })
    this.setData({
        num:5,
    });
    var animation = wx.createAnimation({
      duration: 100,
        timingFunction: 'ease',
    })
    this.animation = animation
    var self=this
    qcloud.request({
            // 要请求的地址
            url: config.service.apiUrl+'posts/'+self.data.hash_key,
            success(result) {
              console.log(result);
              if(result.statusCode==200){
                if(result.data.success==true){
                  if(result.data.is_mine==true){
                      self.setData({
                              is_mine:true
                      });
                  }else{
                      self.setData({
                              is_mine:false
                      });
                  }
                  self.setData({
                          start:true,
                          type:result.data.post.type,
                          text:result.data.post.text,
                          imageSrc:result.data.post.image_url,
                          nicker_name:result.data.post.user.nickname
                  });
                  if(result.data.post.type==1&&result.data.is_mine==false){
                      self.Interval();
                  }else if(result.data.post.type==2){
                    self.setData({
                      loading:true
                    });
                  }
                }else{
                    wx.hideToast();
                    wx.showModal({
                        title:'提示信息',
                        content: result.data.message,
                        showCancel: false,
                        success: function(res) {
                            if (res.confirm) {
                                wx.switchTab({
                                  url: '/pages/channel/index/index'
                                })
                            }
                        }
                    });
                }
              }else{
                 wx.switchTab({
                    url: '/pages/channel/index/index'
                 })
              }
            },

            fail(error) {
                console.log('request fail', error);
            },

            complete() {
                console.log('request complete');
            }
      });
  },
  onShow: function () {
    console.log('onShow');
  },
  imageLoad: function(e){
    console.log(e);
    var self=this;
    self.setData({
            loading:false
    });
    if(self.data.is_mine==false){
        self.Interval();
    }
  },
  play:function(e){
    console.log(e);
  },
  videoPlay:function(){
    this.setData({
        video_hidden:false
    });
    this.videoContext = wx.createVideoContext('myVideo');
    var self=this;
    setTimeout(function(){
        self.videoContext.play();
    }, 500);
  },
  timeUpdate: function(e){
    console.log(e);
    var self=this;
    var num=Math.floor(e.detail.duration)-Math.floor(e.detail.currentTime);
    if(num!==self.data.num){
        self.animation.scale(2, 2).step()
        self.animation.scale(1, 1).step()
        self.setData({
            animationData: self.animation.export()
        })
        self.setData({
            num:num,
        });
    }
    
  },
  ended:function(e){
      var self=this;
      if(self.data.is_mine==false){
        setTimeout(function(){
                wx.switchTab({
                  url: '/pages/channel/index/index'
                })
        }, 1000);
      }
  }
})
